using System;
using System.Collections.Generic;

using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about reminders
    /// </summary>
    public class ReminderDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ReminderDAO));

        /// <summary>
        /// Method responsible by getting all reminders regarding birthday 
        /// </summary>
        /// <returns>IList - list of birthday reminders</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Reminder> findBirthdayReminders()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of reminder value object to be returned
            IList<Reminder> reminderList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                reminderList = ibatisMapper.QueryForList<Reminder>("Reminder.findBirthdayReminders", null);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving reminder information", ex);
                throw new PersistenceException("Error while retrieving reminder information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }

        /// <summary>
        /// Method responsible by getting all reminders regarding medical exam 
        /// </summary>
        /// <returns>IList - list of medical exams reminders</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Reminder> findMedicalExamReminders()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of reminder value object to be returned
            IList<Reminder> reminderList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                reminderList = ibatisMapper.QueryForList<Reminder>("Reminder.findMedicalExamReminders", null);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving reminder information", ex);
                throw new PersistenceException("Error while retrieving reminder information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }

        /// <summary>
        /// Method responsible by getting all reminders regarding monthly fee debtors 
        /// </summary>
        /// <returns>IList - list of monthly fee debtors</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Reminder> findMonthlyFeeDebtorsReminders()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of reminder value object to be returned
            IList<Reminder> reminderList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                reminderList = ibatisMapper.QueryForList<Reminder>("Reminder.findMonthlyFeeDebtorsReminders", null);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving reminder information", ex);
                throw new PersistenceException("Error while retrieving reminder information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }

        /// <summary>
        /// Method responsible by getting all reminders regarding stock item debtors 
        /// </summary>
        /// <param name="Int32">number of days of tolerance for debtors </param>
        /// <returns>IList - list of stock item debtors</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Reminder> findStockItemDebtorsReminders(Int32 debtorsToleranceInDays)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of reminder value object to be returned
            IList<Reminder> reminderList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                reminderList = ibatisMapper.QueryForList<Reminder>("Reminder.findStockItemDebtorsReminders", debtorsToleranceInDays);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving reminder information", ex);
                throw new PersistenceException("Error while retrieving reminder information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }

        /// <summary>
        /// Method responsible by getting all reminders regarding stock item debtors 
        /// </summary>
        /// <param name="Int32">number minimum of items in order to a item be considered as low position</param>
        /// <returns>IList - list of stock item debtors</returns>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public IList<Reminder> findLowStockPositionItemsReminders(Int32 lowPositionItemsQuantity)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // list of reminder value object to be returned
            IList<Reminder> reminderList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                reminderList = ibatisMapper.QueryForList<Reminder>("Reminder.findLowStockPositionItemsReminders", lowPositionItemsQuantity);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving reminder information", ex);
                throw new PersistenceException("Error while retrieving reminder information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return reminderList;
        }
    }
}
